//
//  LSZCollectVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/14.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZCollectVC.h"

#import "LSZCollectTableViewCell.h"

@interface LSZCollectVC ()
@end

@implementation LSZCollectVC
-(void)viewWillAppear:(BOOL)animated
{
    self.pageNum = 1;
    [self collectVCData: self.pageNum];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
-(void)collectVCData :(NSInteger)page
{
    [LSZRequestResolutionDic initWithMyCollection:[NSString stringWithFormat:@"%ld",(long)page]  type:@"colectAdd" :^(BOOL isOk, NSString * str, LSZMyCollectionManager * manager) {
        if (isOk == YES) {
             if (page == 1) {
                [self.collectionArrayData removeAllObjects];
                [self.collectionArrayData setArray:manager.mutDataArray];
                 if(manager.mutDataArray.count == 0){
                     self.tableViewCollect.mj_footer.hidden = YES;
                     LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                     [vc showWithView:self.view];
                 }else{
                     [self.tableViewCollect reloadData];
                 }
            }
            else{
                if(manager.mutDataArray.count == 0){
                    [self.tableViewCollect.mj_footer endRefreshingWithNoMoreData];
                 }else{
                    [self.collectionArrayData addObjectsFromArray:manager.mutDataArray];
                }
                [self.tableViewCollect reloadData];
            }
        }else{
            [self initWithtring:str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"圣经收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setCollectUI];
}
-(void)setCollectUI
{
    [self.tableViewCollect registerClass:[LSZCollectTableViewCell class] forCellReuseIdentifier:@"LSZCollectTableViewCell"];
    //收藏的上下拉
    self.tableViewCollect.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageNum = 1;
         [self collectVCData:self.pageNum];
        [self.tableViewCollect.mj_header endRefreshing];
    }];
    // 上拉刷新
    self.tableViewCollect.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageNum++;
        [self collectVCData:self.pageNum];
        [self.tableViewCollect.mj_footer endRefreshing];
    }];
}

-(UITableView *)tableViewCollect
{
    if (_tableViewCollect == nil) {
        _tableViewCollect = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        _tableViewCollect.delegate = self;
        _tableViewCollect.dataSource = self;
        //添加以下代码关闭估算行高
        _tableViewCollect.estimatedRowHeight = 0;
        _tableViewCollect.estimatedSectionHeaderHeight = 0;
        _tableViewCollect.estimatedSectionFooterHeight = 0;
        _tableViewCollect.separatorColor = divider;
        _tableViewCollect.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewCollect.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.view addSubview:_tableViewCollect];
    }
    return _tableViewCollect;
}
#pragma mark - 有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectionArrayData.count;
}
#pragma mark - 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.collectionArrayData[indexPath.row];
    CGFloat height = [self whatHeightWithRow:collectionMg.content];
    return  height+37;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.collectionArrayData[indexPath.row];
    LSZCollectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZCollectTableViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",collectionMg.title];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",collectionMg.addtime];
    CGFloat height = [self whatHeightWithRow:collectionMg.content];
    CGRect rect2 = cell.contentLabel.frame;
    rect2.size.height = height;
    cell.contentLabel.frame = rect2;
    cell.contentLabel.text = [NSString stringWithFormat:@"%@",collectionMg.content];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.collectionArrayData[indexPath.row];
    if ([collectionMg.title isEqualToString:@"广告"]) {
        return;
    }
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你确定要删除本章节圣经收藏吗?" preferredStyle:UIAlertControllerStyleActionSheet];
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"温馨提示"];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:titleColorLSZ range:NSMakeRange(0, 4)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 4)];
    [alert setValue:alertControllerStr forKey:@"attributedTitle"];
    //修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"你确定要删除本章节圣经收藏吗?"];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:titleColorLSZ range:NSMakeRange(0, 15)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, 15)];
    [alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];

    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        //响应事件
        NSLog(@"action = %@", action);
    }];
    UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action){
        //响应事件
        LSZCollectionMg * collectionMg = nil;
        collectionMg = self.collectionArrayData[indexPath.row];
        [self sendDeleteNote: collectionMg.id_id :indexPath.row];
        NSLog(@"action = %@", action);
    }];
    [alert addAction:cancelAction];
    [alert addAction:deleteAction];
    [self presentViewController:alert animated:YES completion:nil];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        LSZCollectionMg * collectionMg = nil;
        collectionMg = self.collectionArrayData[indexPath.row];
        [self sendDeleteNote: collectionMg.id_id :indexPath.row];
    }
}
-(void)sendDeleteNote :(NSString *)resource_id :(NSInteger)row
{
    [LSZRequestData deleteNoteCollection:resource_id :^(BOOL isoK, NSString * str, NSDictionary * dic) {
        if (isoK == YES) {
            [ self.collectionArrayData removeObjectAtIndex:row];
            [self.tableViewCollect reloadData];
        }else{
            [self initWithtring:str];
        }
    }];
}
#pragma mark - 高度
-(CGFloat)whatHeightWithRow :(NSString *)str
{
    CGRect labTitleRect = [str  boundingRectWithSize:CGSizeMake(self.view.width-2*10, 400) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12.0] forKey:NSFontAttributeName] context:nil];
    return 30+labTitleRect.size.height+7;
}
-(NSMutableArray *)collectionArrayData
{
    if (_collectionArrayData == nil) {
        _collectionArrayData = [[NSMutableArray alloc]init];
    }
    return _collectionArrayData;
}
@end
